package com.codi.api.controller.organ;

import com.codi.api.security.UserUtil;
import com.codi.api.validation.AccountValidation;
import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.MD5;
import com.codi.bus.core.domain.OrganBank;
import com.codi.bus.core.domain.User;
import com.codi.bus.core.fund.FundErrorConst;
import com.codi.bus.core.resultModel.BaseListResult;
import com.codi.bus.core.resultModel.BaseResult;
import com.codi.bus.core.service.QueryService;
import com.codi.bus.core.service.UserService;
import com.codi.bus.core.service.organ.OrganBankService;
import com.codi.bus.core.service.organ.OrganProfileService;
import com.codi.bus.exception.BaseException;
import com.codi.message.constant.ErrorConst;
import com.codi.message.constant.ErrorConstant;
import com.codi.message.util.MessageUtil;
import com.codi.trade.service.LoginAcctService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lvgo
 * @version 1.0
 * @Description: 机构用户银行卡管理
 * @date 2018/5/8 13:51
 */
@RequestMapping("/organ/bank")
@RestController
@Slf4j
public class OrganBankCardController {


    @Resource
    private OrganProfileService organProfileService;
    @Resource
    private OrganBankService organBankService;
    @Resource
    private QueryService queryService;
    @Resource
    private AccountValidation accountValidation;
    @Resource(name = "loginAcctService")
    private LoginAcctService loginAcctService;
    @Resource
    private UserService userService;

    /**
     * 添加银行卡
     *
     * @param profileId   机构id
     * @param bankAccount 银行账户
     * @param bankName    银行名称
     * @param bankNo      银行账号
     * @param branchName  支行名称
     */
    @Loggable
    @PostMapping("/update")
    public BaseResult addOrganBank(String organBankId, String profileId, String tradePassword, String bankAccount, String bankName, String bankNo, String branchName, String bankCode, String branchBank) {
        BaseResult baseResult = new BaseResult();
        String userId = UserUtil.getLoginUserId().toString();
        User user = userService.getUser(UserUtil.getLoginUserId());
        tradePassword = MD5.MD5Encode(tradePassword.trim());
        List<OrganBank> organBanks = organBankService.getListByProfileId(profileId);
        if (organBanks == null || organBanks.isEmpty()) {
            baseResult.setSuccess(false);
            baseResult.setErrorCode(ErrorConstant.ORGAN_PROFILE_NULL);
            baseResult.setErrorMessage(MessageUtil.getErrorMsg(ErrorConstant.ORGAN_PROFILE_NULL));
            return baseResult;
        }
        OrganBank bank = organBanks.get(0);
//        Map<String, Object> map = loginAcctService.login(tradePassword, user.getUserName(), user.getUserCard());
        if (!tradePassword.equals(bank.getTradePassword())) {
            baseResult.setSuccess(false);
            baseResult.setErrorCode(FundErrorConst.TRADE_PWD_WRONG);
            baseResult.setErrorMessage(MessageUtil.getErrorMsg(FundErrorConst.TRADE_PWD_WRONG));
            return baseResult;
        }
        try {
            OrganBank organBank = organBankService.addOrganBank(organBankId, userId, profileId, tradePassword, bankAccount, bankName, bankNo, branchName, bankCode, branchBank);
            baseResult.setResult(organBank);
        } catch (BaseException e) {
            baseResult.setSuccess(false);
            baseResult.setErrorCode(e.getCode());
            baseResult.setErrorMessage(MessageUtil.getErrorMsg(e.getCode()));
            e.printStackTrace();
        }
        return baseResult;
    }


    /**
     * 机构银行卡列表
     */
    @Loggable
    @GetMapping("/list")
    public BaseListResult getOrganBankList(String profileId) {
        BaseListResult baseResult = new BaseListResult();
        try {
            List<OrganBank> organBanks = organBankService.selectAllListByProfileId(profileId);
            if (organBanks != null) {
                baseResult.setTotalCount((long) organBanks.size());
                baseResult.setResult(organBanks);
            } else {
                baseResult.setErrorCode(ErrorConst.ORGAN_BANK_IS_NULL);
                baseResult.setErrorMessage(MessageUtil.getErrorMsg(ErrorConst.ORGAN_BANK_IS_NULL));
            }
        } catch (BaseException e) {
            baseResult.setSuccess(false);
            log.error("获取机构银行卡列表失败，错误信息：{}", e.getMessage());
            baseResult.setErrorCode(e.getCode());
            baseResult.setErrorMessage(MessageUtil.getErrorMsg(e.getCode()));
            e.printStackTrace();
        }
        return baseResult;
    }


}
